﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLite.Reflection;
using NLite.Collections;
using NLite.Mapping.Factories;
using NLite.Mapping.Internal;
using NLite.Mapping;

namespace NLite.Internal
{
    class MapperFactoryRegistry : Dictionary<int, IMapperFactory>, IMapperFactoryRegistry
    {
        public MapperFactoryRegistry()
        {
            Add(0, MapperFactory.StringMapperFactory);
            Add(1, MapperFactory.FlagsEnumMapperFactory);
            Add(2, MapperFactory.EnumMapperFactory);
            Add(3, MapperFactory.NullableMapperFactory);
            Add(4, MapperFactory.PrimitiveMapperFactory);
            Add(5, MapperFactory.TypeConverterMapperFactory);
            Add(6, MapperFactory.DictionaryMapperFactory);
            Add(7, MapperFactory.ClassToDictionaryMapperFactory);
            Add(8, MapperFactory.DictionaryToClassMapperFactory);
            Add(9, MapperFactory.DataReaderMapperFactory);
            Add(10, MapperFactory.DataTableMapperFactory);
            Add(11, MapperFactory.DataRowMapperFactory);
            Add(12, MapperFactory.ListSourceMapperFactory);
            Add(13, MapperFactory.CollectionMapperFactory);
            Add(14, MapperFactory.ClassMapperFactory);
        }
    }

    class MapperRegistry : Dictionary<string, IMapper>, IMapperRegistry
    {
    }

    class MapingEngine : IMappingEngine
    {
        public IMapperRegistry MapperRegistry { get; private set; }
        public IMapperFactoryRegistry FactoryRegistry { get; private set; }

        public MapingEngine() : this(new MapperFactoryRegistry(), new MapperRegistry()) { }
        public MapingEngine(IMapperFactoryRegistry factoryRegistry, IMapperRegistry mapperRegistry)
        {
            FactoryRegistry = factoryRegistry;
            MapperRegistry = mapperRegistry;
        }

        public IMapper<TFrom, TTo> CreateMapper<TFrom, TTo>()
        {
            return (IMapper<TFrom, TTo>)GetOrCreateMap(typeof(TFrom), typeof(TTo));
        }

        private IMapper GetOrCreateMap(Type fromType, Type toType)
        {

            var key = fromType.FullName + "->" + toType.FullName;

            if (MapperRegistry.ContainsKey(key))
                return MapperRegistry[key];

            return CreateMap(fromType, toType, key);
        }

        private IMapper CreateMap(Type fromType, Type toType, string key)
        {
            IMapper mapper = null;
            foreach (var fac in FactoryRegistry.Values.ToArray())
            {
                if (fac.IsMatch(fromType, toType))
                {
                    mapper = CreateGenericMapper(fromType, toType, fac);
                    MapperRegistry.Add(key, mapper);
                    break;
                }
            }

            if(mapper == null)
                throw new NotSupportedException("Mapper does not support " + key + "."); 
            return mapper;
        }

        private static IMapper CreateGenericMapper(Type fromType, Type toType, IMapperFactory fac)
        {
            var mapper = fac.Create(fromType, toType);

            var genericMapperType = typeof(GenericMapper<,>)
             .MakeGenericType(fromType, toType);
            var ctor = genericMapperType.GetConstructor(new Type[] { typeof(MapperBase) });

            var genericMapper = (IMapper)ctor
                .FastInvoke(mapper);

            return genericMapper;
        }
    }
  
}
